package net.bxx2004.extand.manager;

import net.bxx2004.extand.util.JDBCUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MenuManager {
    static Inventory inventory = Bukkit.createInventory(null, ConfigManager.getSetting().getInt("Gui.Size"), ConfigManager.getSetting().getString("Gui.Title"));
    public static void open(Player player) {
        switch (ConfigManager.getSetting().getString("Storge.Type")) {
            case "Yaml":
                a(player);
                break;
            case "MySql":
                String BackPlayer= null;
                String Code = null;
                int Amount= 0;
                try {
                    String sql = "select * from " + ConfigManager.getSetting().getString("Storge.SqlTablename") + " where PlayerName='"+ player.getName() +"'";
                    Connection connection = JDBCUtil.getConnection();
                    PreparedStatement pst = connection.prepareStatement(sql);
                    ResultSet rs  = pst.executeQuery();
                    while(rs.next()){
                        BackPlayer = rs.getString("BackPlayer");
                        Code = rs.getString("Code");
                        Amount = rs.getInt("Amount");
                    }
                    Iterator iterator = ConfigManager.getSetting().getConfigurationSection("Gui.Item").getKeys(false).iterator();
                    while (iterator.hasNext()) {
                        int key = Integer.parseInt((String) iterator.next());
                        ItemStack itemStack = new ItemStack(Material.getMaterial(ConfigManager.getSetting().getString("Gui.Item." + key + ".Material")));
                        ItemMeta im = itemStack.getItemMeta();
                        im.setDisplayName(ConfigManager.getSetting().getString("Gui.Item." + key + ".DisplayName"));
                        List<String> list = new ArrayList<String>();
                        for (String s : ConfigManager.getSetting().getStringList("Gui.Item." + key + ".Lore")) {
                            list.add(s.replaceAll("%Player_Code%", Code)
                                    .replaceAll("%Player_Amount%", String.valueOf(Amount))
                                    .replaceAll("%Player_Back%", BackPlayer));
                        }
                        im.setLore(list);
                        itemStack.setItemMeta(im);
                        inventory.setItem(key, itemStack);
                    }
                    JDBCUtil.close(rs,null, connection, pst);
                    player.openInventory(inventory);
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            default:
                a(player);
                break;
        }
        player.openInventory(inventory);
    }

    public static void a(Player player) {
        Iterator iterator = ConfigManager.getSetting().getConfigurationSection("Gui.Item").getKeys(false).iterator();
        while (iterator.hasNext()) {
            int key = Integer.parseInt((String) iterator.next());
            ItemStack itemStack = new ItemStack(Material.getMaterial(ConfigManager.getSetting().getString("Gui.Item." + key + ".Material")));
            ItemMeta im = itemStack.getItemMeta();
            im.setDisplayName(ConfigManager.getSetting().getString("Gui.Item." + key + ".DisplayName"));
            List<String> list = new ArrayList<String>();
            for (String s : ConfigManager.getSetting().getStringList("Gui.Item." + key + ".Lore")) {
                list.add(s.replaceAll("%Player_Code%", ConfigManager.getConfig().getString(player.getName() + ".Code"))
                        .replaceAll("%Player_Amount%", ConfigManager.getConfig().getString(player.getName() + ".Amount"))
                        .replaceAll("%Player_Back%", ConfigManager.getConfig().getString(player.getName() + ".BackPlayer")));
            }
            im.setLore(list);
            itemStack.setItemMeta(im);
            inventory.setItem(key, itemStack);
        }
        player.openInventory(inventory);
    }
}
